import { Fragment } from 'react';
import { Button, Tooltip } from 'antd';
import { EditOutlined, RollbackOutlined, CloseCircleOutlined, ContainerOutlined } from '@ant-design/icons';

import { ILoadMapTypeAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import pageStyles from '@/common/pageLayout.less';
import { tableMoreBtnConfig } from '@/utils/myutilConfig';
import { getPosIDs, getTenantID, getUserID, tooltipLength } from '@/utils';

import { BidevaluaManageAPI } from '../common/type';
import { ColumnsRenderFun, DictionaryRenderFun, SHTH_CHECKSTATE, getSorter } from '../../Acommon';

/**操作按钮的显示*/
const Operation = (curRes: Record<string, any>, operateFuns: BidevaluaManageAPI.IOperateAPI, record: Record<string, any>) => {
  let operationArray: any = [];

  /** 一个按钮都没有时展示*/
  const Disable = (
    <Fragment key={record?.id + '--Disable'}>
      <Button style={{ color: '#000' }} className="manageBtntTxtStyle" type={'link'} size={'small'} disabled>
        --
      </Button>
    </Fragment>
  );

  /** 更多按钮配置 */
  const otherBtnConfig: ManageCommonAPI.TableMoreBtnConfigApi[] = [
    {
      icon: <EditOutlined />,
      btnName: '编辑',
      clickFnc: operateFuns,
      clickType: 'onEdit',
      isVisible:
        record?.id &&
        ((record?.state == `${getTenantID()}${ILoadMapTypeAll?.EXPERT_BUSINESSTYPE}1000` &&
          !record?.checkState &&
          record?.createPositionID == getPosIDs() &&
          record?.createrID == getUserID()) ||
          (record?.state == `${getTenantID()}${ILoadMapTypeAll?.EXPERT_BUSINESSTYPE}1001` &&
            record?.checkState == SHTH_CHECKSTATE() &&
            record?.createPositionID == getPosIDs() &&
            record?.createrID == getUserID())) &&
        curRes?.['/purchaseSourceExpertController/editRepresentative'],
    },
    // {
    //   icon: <RollbackOutlined />,
    //   btnName: '撤销',
    //   clickFnc: operateFuns,
    //   clickType: 'onInvalid',
    //   isVisible:
    //     curRes['/purchaseSourceExpertController/invalid'] &&
    //     record?.id &&
    //     record?.applyerID == getUserID() &&
    //     record?.applyPositionID == getPosIDs() &&
    //     (record?.state == `${getTenantID()}${ILoadMapTypeAll?.EXPERT_BUSINESSTYPE}1001` ||
    //       (record?.state == `${getTenantID()}${ILoadMapTypeAll?.EXPERT_BUSINESSTYPE}1000` && record?.checkState == SHTH_CHECKSTATE())),
    // },
    {
      icon: <CloseCircleOutlined />,
      btnName: '作废',
      clickFnc: operateFuns,
      clickType: 'onCancel',
      isVisible:
        curRes?.['/purchaseSourceExpertController/cancel'] &&
        record?.state == `${getTenantID()}${ILoadMapTypeAll?.EXPERT_BUSINESSTYPE}1000` &&
        !record?.checkState &&
        record?.createPositionID == getPosIDs() &&
        record?.createrID == getUserID(),
    },
    // {
    //   icon: <ContainerOutlined />,
    //   btnName: 'OA',
    //   clickFnc: operateFuns,
    //   clickType: 'onOAPage',
    //   // isVisible: record?.state == `${getTenantID()}${ILoadMapTypeAll?.EXPERT_BUSINESSTYPE}1001`,
    //   isVisible: true,
    // },
  ];
  const ohterBtn = tableMoreBtnConfig(otherBtnConfig, record);
  operationArray = ohterBtn;
  operationArray = operationArray?.filter((itemBtn: any) => itemBtn);
  return operationArray && operationArray?.length > 0 ? operationArray : [Disable];
};

/**
 * 管理页表格配置
 * */
export const ManageTableSwitch = (curRes: Record<string, any>, operateFuns: BidevaluaManageAPI.IOperateAPI) => {
  return [
    {
      title: '承办部门/团队', // 原【申请部门】
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.createDepartmentName, b?.createDepartmentName),
      width: 170,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '采购项目名称',
      dataIndex: 'purchProjectName',
      key: 'purchProjectName',
      visible: true,
      width: 220,
      fixed: 'left',
      sorter: (a: any, b: any) => getSorter(a?.purchProjectName, b?.purchProjectName),
      render: (text: string, record: Record<string, any>) => {
        return (
          <a
            onClick={() => {
              if (curRes?.['/purchaseSourceExpertController/getSourceExpertById/{id}/{currentUserID}']) {
                operateFuns?.onView(record);
              }
            }}
            style={{ width: '100%' }}
          >
            <Tooltip title={tooltipLength(text, 50)}>
              <span className={pageStyles['over-flow-hidden']}>{text || '--'}</span>
            </Tooltip>
          </a>
        );
      },
    },
    {
      title: '状态',
      dataIndex: 'stateName',
      key: 'stateName',
      sorter: (a: any, b: any) => getSorter(a?.state, b?.state),
      visible: true,
      width: 140,
      render: (text: string) => DictionaryRenderFun({ text, isState: true }),
    },
    {
      title: '评标代表姓名',
      dataIndex: 'fullName',
      key: 'fullName',
      sorter: (a: any, b: any) => getSorter(a?.fullName, b?.fullName),
      visible: true,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '工作单位名称',
      dataIndex: 'companyName',
      key: 'companyName',
      sorter: (a: any, b: any) => getSorter(a?.companyName, b?.companyName),
      visible: true,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    {
      title: '现任职务',
      dataIndex: 'positionName',
      key: 'positionName',
      sorter: (a: any, b: any) => getSorter(a?.positionName, b?.positionName),
      visible: true,
      width: 150,
      render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    },
    // {
    //   title: '资金来源',
    //   dataIndex: 'projectName',
    //   key: 'projectName',
    //   sorter: (a: any, b: any) => getSorter(a?.projectName, b?.projectName),
    //   visible: true,
    //   width: 180,
    //   render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    // },
    // {
    //   title: '资金来源编号',
    //   dataIndex: 'projectCode',
    //   key: 'projectCode',
    //   sorter: (a: any, b: any) => getSorter(a?.projectCode, b?.projectCode),
    //   visible: true,
    //   width: 180,
    //   render: (text: string) => ColumnsRenderFun({ text, isTooltip: true }),
    // },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      key: 'createTime',
      sorter: (a: any, b: any) => getSorter(a?.createTime, b?.createTime),
      width: 150,
      visible: true,
      render: (text: string | null) => {
        return <span>{text ? text?.split(' ')?.[0] : '--'}</span>;
      },
    },
    {
      title: '创建人公司',
      dataIndex: 'createCompanyName',
      key: 'createCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createCompanyID > b?.createCompanyID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人部门',
      dataIndex: 'createDepartmentName',
      key: 'createDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createDepartmentName > b?.createDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人岗位',
      dataIndex: 'createPositionName',
      key: 'createPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.createPositionName > b?.createPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '创建人',
      dataIndex: 'createrName',
      key: 'createrName',
      sorter: (a: any, b: any) => getSorter(a?.createrName, b?.createrName),
      width: 120,
      visible: true,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改时间',
      dataIndex: 'modifyTime',
      key: 'modifyTime',
      visible: false,
      width: 150,
      unDrag: false,
      render: (text: string | null) => {
        return <span>{text ? text?.split(' ')?.[0] : '--'}</span>;
      },
    },
    {
      title: '修改人公司',
      dataIndex: 'modifyCompanyName',
      key: 'modifyCompanyName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyCompanyID > b?.modifyCompanyID ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人部门',
      dataIndex: 'modifyDepartmentName',
      key: 'modifyDepartmentName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyDepartmentName > b?.modifyDepartmentName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人岗位',
      dataIndex: 'modifyPositionName',
      key: 'modifyPositionName',
      visible: false,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.modifyPositionName > b?.modifyPositionName ? 1 : -1),
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '修改人',
      dataIndex: 'modifyerName',
      key: 'modifyerName',
      width: 120,
      visible: false,
      unDrag: false,
      render: (text: string) => ColumnsRenderFun({ text }),
    },
    {
      title: '操作',
      dataIndex: 'operation',
      key: 'operation',
      width: 180,
      visible: true,
      unDrag: true,
      fixed: 'right',
      render: (text: string | null, record: Record<string, any>) => {
        return <div style={{ display: 'flex' }}>{Operation(curRes, operateFuns, record)}</div>;
      },
    },
  ];
};
